package com.fann;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ClassRoomControllerServlet
 */
public class ClassRoomControllerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    private ClassRoomDAO dao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClassRoomControllerServlet() {
        super();
        dao = new ClassRoomDAO();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String acttype = request.getParameter("acttype");
		
		if (acttype.equals("classRoomList")){
			List<ClassRoom> CRs = dao.getAll();
            request.setAttribute("CRs", CRs); 
            
            session.setAttribute("left", "/ClassRoom/classRoomTree.jsp");
			session.setAttribute("center", "/ClassRoom/classRoomList.jsp");
			
		} else if (acttype.equals("classRoomAdd")){
			session.setAttribute("center", "/ClassRoom/classRoomEdit.jsp");

		} else if (acttype.equals("classRoomEdit")){
			ClassRoom CR = dao.getClassRoomById(request.getParameter("id"));
			session.setAttribute("CR", CR);
			
			session.setAttribute("center", "/ClassRoom/classRoomEdit.jsp");
			
		} else if (acttype.equals("classRoomDelete")){
			String id = request.getParameter("id");
			int rowCount =0;
			if (id !=null){
				rowCount = dao.delete(id);
			}
			session.setAttribute("center", "/ClassRoom/classRoomDelete.jsp?rowCount="+ rowCount);
			
		} 

		request.getRequestDispatcher("default.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String acttype = request.getParameter("acttype");
		
		String cr_id, cr_name, cr_location;
		int id, cr_cost;
		
		if (acttype.equals("classRoomAdd")){
			if (request.getParameter("cr_id") != null){
				
						 id =0;
					  cr_id = request.getParameter("cr_id");
					cr_name = request.getParameter("cr_name");
				cr_location = request.getParameter("cr_location");
					cr_cost = Integer.parseInt(request.getParameter("cr_cost"));
				
				//Construction
				ClassRoom CR = new ClassRoom(id, cr_id, cr_name, cr_location, cr_cost);
				
				//add
				dao.add(CR);

				//Success, Fail
				response.sendRedirect("ClassRoomController?acttype=classRoomList");
			} else {
				response.sendRedirect("ClassRoomController?acttype=classRoomAdd");
			}
		}
		
		if (acttype.equals("classRoomEdit")){
			
					 id = Integer.parseInt(request.getParameter("id"));
				  cr_id = request.getParameter("cr_id");
				cr_name = request.getParameter("cr_name");
			cr_location = request.getParameter("cr_location");
				cr_cost = Integer.parseInt(request.getParameter("cr_cost"));
			
			//Construction
			ClassRoom CR = new ClassRoom(id, cr_id, cr_name, cr_location, cr_cost);
			
			//update
		    dao.update(CR);
		    
			//Set List
			List<ClassRoom> CRs = dao.getAll();
            request.setAttribute("CRs", CRs);
            
		    response.sendRedirect("ClassRoomController?acttype=classRoomList");
		}
	}

}
